<?php

class ExpressModel extends Model {
	function __construct(){
		$this->db = parent::dbConnect($GLOBALS ["gDataBase"] ["db"]);
	}

	function getItems($con,$tab,$pageCount){
		/**
		 * 实现查找与分页的功能
		 * @param array $con 查询条件数组
		 * @param string $tab 查询的表名
		 * @param int $pageCount 每页记录数
		 */
		$select = $this->db->select();
		$select->from(" $tab ");

		if(isset($con['a_title']) && !empty($con['a_title'])) $select->where(" a_title like '%".$con['a_title']."%'");
    	if(isset($con['a_sdate']) && !empty($con['a_sdate'])) $select->where(" a_sdate>='".$con['a_sdate']."'");
    	if(isset($con['a_edate']) && !empty($con['a_edate'])) $select->where(" a_edate<='".$con['a_edate']."'");
    	if(isset($con['is_sent'])) $select->where(" is_sent={$con['is_sent']}");
    	if(isset($con['is_signed'])) $select->where(" is_signed={$con['is_signed']}");
    	if(isset($con['app_realname']) && !empty($con['app_realname'])) $select->where(" app_realname like '%".$con['app_realname']."%'");
    	if(isset($con['is_received'])) $select->where(" is_received={$con['is_received']}");

		$total = $select->count();
    	$list = array();

    	//创建分页
    	$pageCount = 10;
    	include_once 'Pager.class.php';
    	$list['page'] = new Pager($total, $pageCount);//创建分页对象
    	$offset = $list['page']->offset();//获得记录偏移量

    	$pagerStyle = array ('firstPage' => 'page', 'prePage' => 'go_b', 'nextPage' => 'go_b','preGroup'=>'page','nextGroup'=>'page', 'totalPage' => '', 'numBar' => 'on', 'numBarMain' => 'page' );                      //翻页条的样式
		$list ['page']->setLinkStyle ( $pagerStyle );
		$label = array('first_page'=>lang('first_page'),'last_page'=>lang('last_page'),'next_page'=>lang('next_page'),'pre_page'=>lang('pre_page'),'next_group'=>lang('next_group'),'pre_group'=>lang('pre_group'));
		$list ['page']->setLabelName($label);
		if($total>$pageCount){
			$list ['pagebar'] = $list ['page']->prePage (lang('pre_page') ) .$list ['page']->ocNumBar().$list ['page']->nextPage (lang('next_page'));
		}else{
			$list ['pagebar'] = '';
		}

		$select->limit ( $list['page']->offset(), $pageCount );
		$rs = $select->query();
		if($rs){
    		foreach($rs as $key => $record){
    			$list['records'][$key] = $record;
    		}
    	}else $list['records'] = '';

    	return (array) $list;
	}

	function createNewExpress($exp){
		return $this->db->execute("insert into express (a_id, user_id, a_title, a_sdate, a_edate, app_realname, app_mobile, app_postaddress) values (".$exp['a_id'].", ".$exp['user_id'].", '{$exp['a_title']}', '{$exp['a_sdate']}', '{$exp['a_edate']}', '{$exp['app_realname']}', '{$exp['app_mobile']}', '{$exp['app_postaddress']}')");
	}

	function updateExpress($item,$a_id){
		return $this->db->update($item,'express'," a_id=".$a_id);
	}

	function getExpressById($ex_id){
		return $this->db->getRow("select * from express e where e.ex_id=$ex_id");
	}

	function getAllExpress(){
		$sql = "select * from express";
		return $this->db->getAll($sql);
	}

	function getSendExpress(){
		$sendexpress = $this->db->getAll("select * from express e where e.is_sent=0");
		$total = count($sendexpress);
		$list = array();
		$pageCount = 10;
		//新建分页对象
		include_once "Pager.class.php";
		$list['page'] = new Pager($total,$pageCount);
		$offset = $list['page']->offset();//获得记录偏移量

    	$pagerStyle = array ('firstPage' => 'page', 'prePage' => 'go_b', 'nextPage' => 'go_b','preGroup'=>'page','nextGroup'=>'page', 'totalPage' => '', 'numBar' => 'on', 'numBarMain' => 'page' );                      //翻页条的样式
		$list ['page']->setLinkStyle ( $pagerStyle );
		$label = array('first_page'=>lang('first_page'),'last_page'=>lang('last_page'),'next_page'=>lang('next_page'),'pre_page'=>lang('pre_page'),'next_group'=>lang('next_group'),'pre_group'=>lang('pre_group'));
		$list ['page']->setLabelName($label);
		if($total>$pageCount){
			$list ['pagebar'] = $list ['page']->prePage (lang('pre_page') ) .$list ['page']->ocNumBar().$list ['page']->nextPage (lang('next_page'));
		}else{
			$list ['pagebar'] = '';
		}

    	//将$list['pagebar']安插进结果集中返回
    	$rs = $this->db->getAll("select * from express e where e.is_sent=0 limit ".$list['page']->offset().", ".$pageCount);
    	if($rs){
    		foreach($rs as $key => $record){
    			$list['records'][$key] = $record;
    		}
    	}else $list['records'] = '';

    	return (array) $list;
	}

	function getSentExpress(){
		$sendexpress = $this->db->getAll("select * from express e where e.is_sent=1 and e.is_signed=0");
		$total = count($sendexpress);
		$list = array();
		$pageCount = 10;
		//新建分页对象
		include_once "Pager.class.php";
		$list['page'] = new Pager($total,$pageCount);
		$offset = $list['page']->offset();//获得记录偏移量

    	$pagerStyle = array ('firstPage' => 'page', 'prePage' => 'go_b', 'nextPage' => 'go_b','preGroup'=>'page','nextGroup'=>'page', 'totalPage' => '', 'numBar' => 'on', 'numBarMain' => 'page' );                      //翻页条的样式
		$list ['page']->setLinkStyle ( $pagerStyle );
		$label = array('first_page'=>lang('first_page'),'last_page'=>lang('last_page'),'next_page'=>lang('next_page'),'pre_page'=>lang('pre_page'),'next_group'=>lang('next_group'),'pre_group'=>lang('pre_group'));
		$list ['page']->setLabelName($label);
		if($total>$pageCount){
			$list ['pagebar'] = $list ['page']->prePage (lang('pre_page') ) .$list ['page']->ocNumBar().$list ['page']->nextPage (lang('next_page'));
		}else{
			$list ['pagebar'] = '';
		}

    	//将$list['pagebar']安插进结果集中返回
    	$rs = $this->db->getAll("select * from express e where e.is_sent=1 and e.is_signed=0 limit ".$list['page']->offset().", ".$pageCount);
    	if($rs){
    		foreach($rs as $key => $record){
    			$list['records'][$key] = $record;
    		}
    	}else $list['records'] = '';

    	return (array) $list;
	}

	function getAssignmentInfoById($a_id){
		return $this->db->getRow("select a.a_id, a.a_title, a.a_sdate, a.a_edate from assignment a where a.a_id=$a_id");
	}

	function getApplicantInfoById($user_id){
		return $this->db->getRow("select u.user_id, u.realname, u.mobile, u.postaddress from user_ext u where u.user_id=$user_id");
	}

	function updateSigned($item,$ex_id){
		return $this->db->update($item,'express'," ex_id=".$ex_id);
	}

	function getSignedExpress(){
		$sendexpress = $this->db->getAll("select * from express e where e.is_signed=1");
		$total = count($sendexpress);
		$list = array();
		$pageCount = 10;
		//新建分页对象
		include_once "Pager.class.php";
		$list['page'] = new Pager($total,$pageCount);
		$offset = $list['page']->offset();//获得记录偏移量

    	$pagerStyle = array ('firstPage' => 'page', 'prePage' => 'go_b', 'nextPage' => 'go_b','preGroup'=>'page','nextGroup'=>'page', 'totalPage' => '', 'numBar' => 'on', 'numBarMain' => 'page' );                      //翻页条的样式
		$list ['page']->setLinkStyle ( $pagerStyle );
		$label = array('first_page'=>lang('first_page'),'last_page'=>lang('last_page'),'next_page'=>lang('next_page'),'pre_page'=>lang('pre_page'),'next_group'=>lang('next_group'),'pre_group'=>lang('pre_group'));
		$list ['page']->setLabelName($label);
		if($total>$pageCount){
			$list ['pagebar'] = $list ['page']->prePage (lang('pre_page') ) .$list ['page']->ocNumBar().$list ['page']->nextPage (lang('next_page'));
		}else{
			$list ['pagebar'] = '';
		}

    	//将$list['pagebar']安插进结果集中返回
    	$rs = $this->db->getAll("select * from express e where e.is_signed=1 limit ".$list['page']->offset().", ".$pageCount);
    	if($rs){
    		foreach($rs as $key => $record){
    			$list['records'][$key] = $record;
    		}
    	}else $list['records'] = '';

    	return (array) $list;
	}

	function getReceiveExpress(){
		$sendexpress = $this->db->getAll("select * from express e where e.is_signed=1 and e.is_received=0");
		$total = count($sendexpress);
		$list = array();
		$pageCount = 10;
		//新建分页对象
		include_once "Pager.class.php";
		$list['page'] = new Pager($total,$pageCount);
		$offset = $list['page']->offset();//获得记录偏移量

    	$pagerStyle = array ('firstPage' => 'page', 'prePage' => 'go_b', 'nextPage' => 'go_b','preGroup'=>'page','nextGroup'=>'page', 'totalPage' => '', 'numBar' => 'on', 'numBarMain' => 'page' );                      //翻页条的样式
		$list ['page']->setLinkStyle ( $pagerStyle );
		$label = array('first_page'=>lang('first_page'),'last_page'=>lang('last_page'),'next_page'=>lang('next_page'),'pre_page'=>lang('pre_page'),'next_group'=>lang('next_group'),'pre_group'=>lang('pre_group'));
		$list ['page']->setLabelName($label);
		if($total>$pageCount){
			$list ['pagebar'] = $list ['page']->prePage (lang('pre_page') ) .$list ['page']->ocNumBar().$list ['page']->nextPage (lang('next_page'));
		}else{
			$list ['pagebar'] = '';
		}

    	//将$list['pagebar']安插进结果集中返回
    	$rs = $this->db->getAll("select * from express e where e.is_signed=1 and e.is_received=0 limit ".$list['page']->offset().", ".$pageCount);
    	if($rs){
    		foreach($rs as $key => $record){
    			$list['records'][$key] = $record;
    		}
    	}else $list['records'] = '';

    	return (array) $list;
	}

	function getReceivedExpress(){
		$sendexpress = $this->db->getAll("select * from express e where e.is_signed=1 and e.is_received=1");
		$total = count($sendexpress);
		$list = array();
		$pageCount = 10;
		//新建分页对象
		include_once "Pager.class.php";
		$list['page'] = new Pager($total,$pageCount);
		$offset = $list['page']->offset();//获得记录偏移量

    	$pagerStyle = array ('firstPage' => 'page', 'prePage' => 'go_b', 'nextPage' => 'go_b','preGroup'=>'page','nextGroup'=>'page', 'totalPage' => '', 'numBar' => 'on', 'numBarMain' => 'page' );                      //翻页条的样式
		$list ['page']->setLinkStyle ( $pagerStyle );
		$label = array('first_page'=>lang('first_page'),'last_page'=>lang('last_page'),'next_page'=>lang('next_page'),'pre_page'=>lang('pre_page'),'next_group'=>lang('next_group'),'pre_group'=>lang('pre_group'));
		$list ['page']->setLabelName($label);
		if($total>$pageCount){
			$list ['pagebar'] = $list ['page']->prePage (lang('pre_page') ) .$list ['page']->ocNumBar().$list ['page']->nextPage (lang('next_page'));
		}else{
			$list ['pagebar'] = '';
		}

    	//将$list['pagebar']安插进结果集中返回
    	$rs = $this->db->getAll("select * from express e where e.is_signed=1 and e.is_received=1 limit ".$list['page']->offset().", ".$pageCount);
    	if($rs){
    		foreach($rs as $key => $record){
    			$list['records'][$key] = $record;
    		}
    	}else $list['records'] = '';

    	return (array) $list;
	}

	function getItemsWithNopage($con,$table){
		/**
		 * 实现不分页的信息查找
		 */
		 $select = $this->db->select();
		 $select->from(" $table ");

		 if(isset($con['a_sdate']) && !empty($con['a_sdate'])) $select->where(" a_sdate>='".$con['a_sdate']."'");
    	 if(isset($con['a_edate']) && !empty($con['a_edate'])) $select->where(" a_edate<='".$con['a_edate']."'");

    	 $list = array();
    	 $list = $select->query();

    	 return $list;
	}

}
?>